package com.controller.infocenter;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.MyUploadInfo;
import com.service.infocenter.MyUploadService;
import com.util.StringFinal;


@Controller
@RequestMapping("/jsp/infocenter")
public class MyuploadController {
    @Resource(name="myUploadServiceImp")
    private MyUploadService myUploadService;
    
    @RequestMapping(value="/getUploadview")
    public String getUploadview(){
    	
    	return "infocenter/myupload";
    }
    
    @RequestMapping(value="/getMaintenanceview")
    public String getMaintenanceview(){
    	
    	return "infocenter/maintenance";
    }
	@ResponseBody
	@RequestMapping(value="/getUpload")
	public Map<String, Object> getUpload(int limit,int offset,String departmentname,String status, HttpServletRequest request){
		String uploaduser = request.getSession().getAttribute("user_id").toString();
		MyUploadInfo myUploadInfo=new MyUploadInfo();
		myUploadInfo.setUser_id(uploaduser);
		myUploadInfo.setName(departmentname);
		myUploadInfo.setStatus(status);
		myUploadInfo.setPage(offset);
		myUploadInfo.setRows(limit);
		int count=myUploadService.getMyUploadCount(myUploadInfo);
		List<MyUploadInfo> list=myUploadService.getMyUploadList(myUploadInfo);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("total", count);
		map.put("rows", list);
		return map;
	}
	@ResponseBody
	@RequestMapping(value="/deleteUpload")
	public int deleteUpload(MyUploadInfo myUploadInfo,String zt,HttpServletRequest request){
		int i=myUploadService.deleteMyUpload(myUploadInfo);
		if(i>0){
			StringBuffer buffer = new StringBuffer(request.getServletContext().getRealPath(myUploadInfo.getInfourl()));
			File delfile = new File(buffer.toString());
			if(delfile.exists()){
				delfile.delete();
			}
			else{
				i=0;
			}	
		}
		return i;
	}
	@ResponseBody
	@RequestMapping(value="/deleteUploads")
	public int deleteUploads(String ids,@RequestParam("urls")String[] urls,@RequestParam("names")String[] names,String zt,@RequestParam("kutypes")String[] kutypes,HttpServletRequest request){
		String[] id = ids.split(",");
		int i=myUploadService.deleteMyUploads(id,urls,request.getServletContext().getRealPath(""));
		return i;
	}
	
	/**
	 * 知识维护员 删除
	 * @param myUploadInfo
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteUploady")
	public int deleteUploady(MyUploadInfo myUploadInfo,HttpServletRequest request){
		int i=myUploadService.deleteMyUpload(myUploadInfo);
		//zt用来判断是从我的上传还是知识管理传过来的请求
		if(i>0&&myUploadInfo.getStatus().equals("通过")){
			String userid = request.getSession().getAttribute("user_id").toString();
			myUploadInfo.setUser_id(userid);
			myUploadService.addInfodelete(myUploadInfo);
		}
		if(i>0){
			StringBuffer buffer = new StringBuffer(request.getServletContext().getRealPath(myUploadInfo.getInfourl()));
			File delfile = new File(buffer.toString());
			if(delfile.exists()){
				delfile.delete();
				//用于文件预览
				String pf = myUploadInfo.getInfourl().split("\\.")[1];
				if(myUploadInfo.getStatus().equals("通过")&&pf.contains("doc")||pf.contains("ppt")||pf.contains("xls"))
				{
					StringBuffer viewbuffer = new StringBuffer(request.getServletContext().getRealPath(StringFinal.viewpath+myUploadInfo.getInfourl().split("/")[1].split("\\.")[0]+".pdf"));
					File delviewfile = new File(viewbuffer.toString());
					if(delviewfile.exists())
						delviewfile.delete();
					viewbuffer.setLength(0);
				}
			}
			else{
			i=0;
			}	
		}
		return i;
	}
	/**
	 * 知识维护员 删除
	 * @param myUploadInfo
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteUploadsy")
	public int deleteUploadsy(String delids,@RequestParam("delurls")String[] delurls,String ids,@RequestParam("urls")String[] urls,@RequestParam("names")String[] names,@RequestParam("kutypes")String[] kutypes,HttpServletRequest request){
		String[] delid = delids.split(",");
		int i=myUploadService.deleteMyUploads(delid,urls,request.getServletContext().getRealPath(""));
		if(i>0){
		        String[] id = ids.split(",");
			String userid = request.getSession().getAttribute("user_id").toString();
			myUploadService.addInfodeleteList(id, urls, names, kutypes, userid);
		}
		return i;
	}
}
